package com.qianfeng.es.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.es.service.IndexService;
import com.qianfeng.es.utils.ConnectionUtil;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;

import java.io.IOException;

/**
 * Created by Jackiechan on 2022/2/16/21:27
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
public class IndexServiceImpl implements IndexService {
    RestHighLevelClient client = ConnectionUtil.getClient();

    @Override
    public void createIndex(String name) throws Exception {

        if (indexExist(name)) {
            System.err.println("index已经存在");
            return;
        }
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(name);
        //mapping的builder
        XContentBuilder xContentBuilder = JsonXContent.contentBuilder().startObject()
                .startObject("properties")
                .startObject("name").field("type", "text").field("analyzer", "ik_max_word").endObject()
                .startObject("author").field("type", "keyword").endObject()
                .startObject("count").field("type", "long").endObject()
                .startObject("onsale").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                .startObject("descr").field("type", "text").field("analyzer", "ik_max_word").endObject()
                .endObject()
                .endObject();


        createIndexRequest.
                settings(Settings.builder().put("number_of_shards",3).put("number_of_replicas", 2))//设置分配
                .mapping(xContentBuilder);
        CreateIndexResponse response = client.indices()//和index相关的操作
                .create(createIndexRequest, RequestOptions.DEFAULT);//创建库
        System.err.println(response);
    }

    @Override
    public boolean indexExist(String indexName) throws Exception {
        GetIndexRequest request = new GetIndexRequest(indexName);
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);//返回true代表存在
        return exists;
    }

    @Override
    public void deleteIndex(String indexName) throws Exception {
        DeleteIndexRequest request = new DeleteIndexRequest(indexName);
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        System.err.println(response);
    }
}
